// pages/addProduct/addProduct.js
import config from '../../utils/config'
import request from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    category: '请选择分类',
    categoryId: null,
    name: '',
    barcode: '',
    imgUrl: null,
    retailPrice: null,
    wholesalePrice: null,
    scaPrice: null,
    show: false,
    categoryList: [],
  },

  // 选择分类
  checkCategory() {
    this.setData({ show: true })
  },
  // 上传文件获取返回地址
  uploadFile() {
      const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.host + '/common/uploadFile', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          header: {
            Authorization: wx.getStorageSync('token')
          },
          name: 'file',
          success(res) {
            if (JSON.parse(res.data).code == 200) {
              that.setData({ imgUrl: "http://"+JSON.parse(res.data).data + "?imageView2/2/w/120"})
            }else if (JSON.parse(res.data).code == 401){
              let code2session = wx.getStorageSync('code2session')
          const para = {
            openid: code2session.openid
          }
          wx.request({
            url: config.host + '/admin/openidLogin',
            data: para,
            method: 'POST',
            success: (res) => {
              let token = res.data.data.tokenHead + res.data.data.token
              wx.setStorageSync('token', token)
            }
          })
            }
          }
        })
      }
    })
  },
  //   获取分类列表
  getCategoryList() {
    request('/smile/smileCategory/getAllList').then((res) => {
      this.setData({ categoryList: res })
    })
  },
  // 关闭选择框
  onClose() {
    this.setData({ show: false })
  },
  // 扫码
  handleScan() {
    let { barcode } = this.data
    const that = this
    wx.scanCode({
      success(res) {
        barcode = res.result
        that.setData({ barcode })
      }
    })
  },
    // 保存商品
    saveProduct(){
      wx.showLoading({
        title:"请求中"
      })
      const { id, categoryId, name, imgUrl, retailPrice, scaPrice, barcode, wholesalePrice} = this.data;
      let para = {
        id, categoryId, name, retailPrice, scaPrice, barcode, wholesalePrice,
        image: imgUrl
      }
      request('/smile/smileProduct/createOrUpdate', para, 'POST').then((res) => {
      wx.hideLoading()
        wx.navigateBack()
      })
    },

  onSelect(event) {
    let { category } = this.data
    console.log(event)
    category = event.detail.name
    this.setData({
      category,
      categoryId: event.detail.id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getCategoryList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})
